let mysqlModule = require("mysql");
let db = require("../config/db");
// let connection = null;
class mysql {
    // constructor() {
    // connection = mysqlModule.createConnection({ host: '127.0.0.1', user: 'root', password: '123456', database: 'yiqing' });
    // }

    query(sql, value) {
        let promise = new Promise(function (resolve, reject) {
            let connection = mysqlModule.createConnection({
                host: db.host,
                user: db.user,
                password: db.password,
                database: db.database
            });
            connection.connect();   //打开连接
            connection.query(sql, value, function (err, result) {
                if (err) {
                    console.log(err.message);
                    //reject(err.message)
                } else {
                    //setTimeout(function () {//模拟数据的慢查询
                    resolve(result);
                    //},5000)
                }
                connection.end();   //关闭连接
            })

        })
        return promise;
    }
     select(condition) {
        let sql = "select * from " + this.table;
        return this.query(sql, '');
    }
}

module.exports = mysql;